package com.limox.bishe.activity;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
import com.limox.bishe.R;
import com.limox.bishe.db.DataBaseManager;
import com.limox.bishe.viewgroup.Classroom;
import com.limox.bishe.viewgroup.ClassroomLeft;
import com.limox.bishe.viewgroup.ClassroomRight;
 
import android.app.FragmentTransaction;
import android.content.Intent;
import android.content.res.Resources;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.ListFragment;
import android.view.MenuItem;
import android.view.Window;

public class ClassroomActivity extends SlidingFragmentActivity implements
		ClassroomLeft.OnListItemClickListenner,Classroom.OnRefesh {
	protected ClassroomLeft mFrag;
	private SQLiteDatabase SQLdb;
	private SlidingMenu sm;

	// 接受信息更新界面
	Handler myHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				sm.toggle();
				break;
			}

			super.handleMessage(msg);
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// set the Behind View
		// 获取intent过来的数据
		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		String teacher = bundle.getString("teacher");
		String courname = bundle.getString("courname");
		String classroom = bundle.getString("classroom");

		// 初始化设置
		sm = getSlidingMenu();
		sm.setShadowDrawable(R.drawable.shadow);
		sm.setFadeDegree(0.2f);
		sm.setShadowWidth(20);

		sm.setBehindOffset(200);
		sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		sm.setMode(SlidingMenu.LEFT);

		sm.setSecondaryShadowDrawable(R.drawable.shadowright);

		// 左抽屉
		setBehindContentView(R.layout.framelayout_left);
		if (savedInstanceState == null) {
			FragmentTransaction t = getFragmentManager().beginTransaction();
			mFrag = new ClassroomLeft(teacher, courname, classroom);
			t.replace(R.id.menu, mFrag);
			t.commit();
		} else {
			mFrag = (ClassroomLeft) this.getFragmentManager().findFragmentById(
					R.id.menu);
		}

		// 主界面
		setContentView(R.layout.framelayout_classroom);
		getFragmentManager()
				.beginTransaction()
				.replace(R.id.content,
						new Classroom(classroom, courname, teacher)).commit();

		// // 右抽屉
		// getSlidingMenu().setSecondaryMenu(R.layout.framelayout_right);
		// getFragmentManager().beginTransaction()
		// .replace(R.id.menu_right, new ClassroomRight()).commit();

		// ACTIONBAR
		getActionBar().setDisplayHomeAsUpEnabled(true);
		getActionBar().setTitle(courname);
		// getActionBar().setIcon(R.drawable.actionnoimg);
		Resources r = getResources();
		Drawable myDrawable = r.getDrawable(R.drawable.topbg);
		getActionBar().setBackgroundDrawable(myDrawable);

		// 创建数据库
		// 数据库如果不存在则建数据库
		SQLdb = DataBaseManager.OpenDb(ClassroomActivity.this, "kb", null, 3);

		// 创建数据库
		DataBaseManager.CreatTable_Note(SQLdb);

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	public void onSelect(String id,String title, String note) {
		Classroom classfragment = (Classroom) getFragmentManager()
				.findFragmentById(R.id.content);
		if (classfragment != null) {
			classfragment.setEdittext(note);
			classfragment.setTitle(title);
			classfragment.setId(id);
//			sm.toggle();

		}

	}

	public void toggle() {
		new Thread(new myThread()).start(); // 首次启动打开主界面，直接打开打开不了，所用用了定时后打开
	}
	
	

	class myThread implements Runnable {
		public void run() {
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				Thread.currentThread().interrupt();
			}
			Message message = new Message();
			message.what = 1;
			myHandler.sendMessage(message);

		}
	}



	@Override
	public void reflesh() {
		// TODO Auto-generated method stub
		ClassroomLeft classfragmentleft = (ClassroomLeft) getFragmentManager()
				.findFragmentById(R.id.menu);
		if (classfragmentleft != null) {
			classfragmentleft.refesh();
			
		}
		
		
	}
	

}
